﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Tags;
using System.Text.RegularExpressions;
using seo.Buz;
using seo.Data;

namespace seo.util
{
    public class YiLookCollector : Collector
    {
        private string homepage = "http://www.yi-look.com/";

        private const int maxpage = 110;

        private YiLookBuz buz = new YiLookBuz();

        public override void Collect()
        {
            throw new NotImplementedException();
        }

        public void collectArts()
        {

        }

        public void GetContentTest()
        {
            List<YiLookArt> arts = buz.GetAllArts();
            foreach (YiLookArt art in arts)
            {
                List<YiLookChapter> chapters = buz.GetChaptersByArt(art.id);
                foreach (YiLookChapter chapter in chapters)
                {
                    String content = buz.GetContentByChapter(chapter);
                    return;
                }
            }
        }

        public void CollectAllContent()
        {
            List<YiLookArt> arts = buz.GetAllArts();
            foreach (YiLookArt art in arts)
            {
                List<YiLookChapter> chapters = buz.GetChaptersByArt(art.id);
                HtmlAnalyzer ha = new HtmlAnalyzer();
                GetContent getContent = new GetContent();
                HttpHelper hh = new HttpHelper();
                foreach (YiLookChapter chapter in chapters)
                {
                    String url = homepage + chapter.url;
                    String html = hh.GetPage(url, String.Empty);
                    html = getContent.getContentUsingParser(html);
                    html = html.Substring(0, html.LastIndexOf("\n"));//去掉yi-look文章最后一段版权说明
                    buz.InsertContent(chapter, html);
                    return;
                }
            }
        }
        
        public void CollectArtsFromCatalog(YiLookCatalog catalog)
        {
            for (int i = 1; i <= catalog.page_count; i++)
            {
                List<YiLookArt> list = GetArtsFromCatalog(catalog,i);
                foreach (YiLookArt art in list)
                {
                    buz.InsertArt(art);
                }
            }
        }

        public List<YiLookCatalog> CollectCatalogs()
        {

            List<YiLookCatalog> list = buz.GetAllCatalog();

            int count;
            foreach (YiLookCatalog catalog in list) {
                count = this.getCatalogPageCount(catalog);

                if (count != 0 && count > catalog.page_count) {
                    catalog.page_count = count;
                    buz.updateCatalogPageCount(catalog);
                }
            }

            return list;
        }

        public List<YiLookArt> GetAllArts()
        {

            List<YiLookArt> list = buz.GetAllArts();

            return list;
        }

        public int getCatalogPageCount(YiLookCatalog catalog)
        {
            int count=0;

            string url = homepage + catalog.url;
            HttpHelper hh = new HttpHelper();
            string html = hh.GetPage(url, string.Empty);

            string regex = @"第\s*\d+\s*\/\s*(\d+)\s*页";
            Regex r = new Regex(regex, RegexOptions.IgnoreCase);
            Match m = r.Match(html);
            if (m.Success)
            {
                count = int.Parse(m.Groups[1].Value);

            }


           return count;
        }

        public void CollectArtsFromCatalog(string url)
        {

            
        }

 /*       public void collectArtsFromPage(int pageIndex)
        {
            string url = homepage;
            if (pageIndex > 1)
            {
                url += String.Format("artlist_{0}.html", pageIndex);
            }
        }*/

        public void collectChapters(YiLookArt art)
        {
           string url = homepage + art.url;
            Hashtable ht = GetUrlsFromArt(url);

            int maxChapterid = buz.getMaxChapterId(art.id);

            foreach (DictionaryEntry entry in ht)
            {
                if ((int)entry.Key > maxChapterid) {
                    YiLookChapter chapter = new YiLookChapter();
                    chapter.artid = art.id;
                    chapter.chapterid = (int)entry.Key;
                    chapter.url = (string)entry.Value;
                    chapter.upload_flg = "0";

                    buz.InsertChapter(chapter);
                }
            }
        }

        public List<YiLookArt> GetArtsFromCatalog(YiLookCatalog catalog, int page_count)
        {
            string url = homepage + catalog.url;
            if (page_count > 1) {
                url = url.Substring(0,url.LastIndexOf("."));
                url += String.Format("_{0}.html", page_count);
            }
            HttpHelper hh = new HttpHelper();
            string html = hh.GetPage(url,string.Empty);
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(TableRow));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            List<YiLookArt> artlist = new List<YiLookArt>();
            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                TableRow tr = (TableRow)node;
                if (tr.ColumnCount == 4 && tr.Columns[3].ToHtml().Contains("[点击阅读]"))
                {
                    YiLookArt art = new YiLookArt();
                    ATag a = (ATag)tr.Columns[0].ChildAt(0).LastChild;
                    string name;
                    string regex = @"《(.*)》";
                    Regex r = new Regex(regex, RegexOptions.IgnoreCase);
                    Match m = r.Match(a.ToPlainTextString());
                    if (m.Success)
                    {
                        name = m.Groups[1].Value;
                        art.name = name;
                    }
                    if (tr.Columns[2].ToPlainTextString().Contains("全文完"))
                    {
                        art.finish_flg = "1";
                    }
                    else { art.finish_flg = "0"; }
                    art.cat_id = catalog.id;
                    art.url = a.Link;
                    artlist.Add(art);
                }
            }
            return artlist;
        }

        public List<YiLookArt> GetUrlsFromHomePage(string url)
        {
            url = homepage + url;
            HttpHelper hh = new HttpHelper();
            string html = hh.GetPage(url, string.Empty);
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(TableRow));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            List<YiLookArt> artlist = new List<YiLookArt>();
            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                TableRow tr = (TableRow)node;
                if (tr.ColumnCount == 4 && tr.Columns[3].ToHtml().Contains("[点击阅读]"))
                {
                    YiLookArt art = new YiLookArt();
                    ATag a = (ATag)tr.Columns[0].ChildAt(0).FirstChild;
                    string name;
                    string regex = @"《(.*)》";
                    Regex r = new Regex(regex, RegexOptions.IgnoreCase);
                    Match m = r.Match(a.ToPlainTextString());
                    if (m.Success)
                    {
                        name = m.Groups[1].Value;
                        art.name = name;
                    }
                    if (tr.Columns[2].ToPlainTextString().Contains(""))
                    {
                        art.finish_flg = "1";
                    }
                    else { art.finish_flg = "0"; }
                    artlist.Add(art);
                }
            }
            return artlist;
        }

        public Hashtable GetUrlsFromArt(string url)
        {
            Hashtable ht = new Hashtable();
            HttpHelper hh = new HttpHelper();
            string html = hh.GetPage(url, string.Empty);
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(ATag));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                ATag a = (ATag)node;

                int chapter = 0;
                string regex = @"第(\d+)节";
                Regex r = new Regex(regex, RegexOptions.IgnoreCase);
                Match m = r.Match(a.ToPlainTextString());
                if (m.Success)
                {
                    chapter = int.Parse(m.Groups[1].Value);
                    if (!ht.ContainsKey(chapter)) ht.Add(chapter, a.Link);
                }
            }
            return ht;
        }
    }
}
